package de.gameduell.framework.gui.animation {
	import fl.transitions.TweenEvent;
	
	import flash.display.Shape;
	import flash.events.EventDispatcher;
	import flash.geom.Point;		

	/**
	 * @author cbra
	 */
	public class PaintTween extends EventDispatcher implements Tweenable {
		private var shape:Shape;
		private var startPoint:Point;
		private var endPoint:Point;
		private var interpolationTween:GDTween;
		private var lineStyle:Array;
		private var _position:Point;

		public function PaintTween(aShape:Shape,aLineStyle:Array,EaseType:Function,aStartPoint:Point,aEndPoint:Point,duration:Number, useSeconds:Boolean) {		
			startPoint = aStartPoint;
			endPoint = aEndPoint;
			shape = aShape;
			lineStyle = aLineStyle;
			interpolationTween = new GDTween(this,"interpolation", EaseType,0,1,duration,useSeconds);
			interpolationTween.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
			interpolationTween.addEventListener(TweenEvent.MOTION_CHANGE, onMotionChange);
		}
		
		private function onMotionChange(event:TweenEvent):void {
			dispatchEvent(new TweenEvent(TweenEvent.MOTION_CHANGE,event.time,event.position,event.bubbles,event.cancelable));
		}

		//thickness:Number = NaN, color:uint = 0, alpha:Number = 1.0, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3
		public function setLineStyle(thickness:Number = NaN, color:uint = 0, alpha:Number = 1.0, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3):void{
			lineStyle = new Array(thickness,color,alpha,pixelHinting,scaleMode,caps,joints,miterLimit);
		}
		
		private function onMotionFinished(event:TweenEvent):void {
			dispatchEvent(new TweenEvent(event.type, event.time, event.position, event.bubbles, event.cancelable));
		}

		public function set interpolation(factor:Number):void{		
			_position = Point.interpolate(endPoint, startPoint, factor);
			shape.graphics.clear();	
			//shape.graphics.lineStyle(5, 0, 0.3);
			applyLineStyle();
			shape.graphics.moveTo(startPoint.x, startPoint.y);
			shape.graphics.lineTo(_position.x, _position.y);			
		}	
		
		public function get position():Point{
			return _position;
		}
			
		public function applyLineStyle():void{
			var thickness:Number = lineStyle[0] != undefined ? lineStyle[0] : NaN;
			var color:uint = lineStyle[1] != undefined ? lineStyle[1] : 0;
			var alpha:Number = lineStyle[2] != undefined ? lineStyle[2] : 1;
			var pixelHinting:Boolean = lineStyle[3] != undefined ? lineStyle[3] : false;
			var scaleMode:String = lineStyle[4] != undefined ? lineStyle[4] : "normal";
			var caps:String = lineStyle[5] != undefined ? lineStyle[5] : null;
			var joints:String = lineStyle[6] != undefined ? lineStyle[6] : null;
			var miterLimit:Number = lineStyle[7] != undefined ? lineStyle[7] : 3;
			shape.graphics.lineStyle(thickness, color, alpha,pixelHinting,scaleMode,caps,joints,miterLimit);
		}
		
		public function start():void {
			shape.graphics.clear();	
			shape.graphics.moveTo(startPoint.x, startPoint.y);
			interpolationTween.start();		
		}
		
		public function stop():void {
			interpolationTween.stop();
		}
		
		public function get isPlaying():Boolean {
			return interpolationTween.isPlaying;
		}
	}
}



